// import Vuex, { ActionObject, ActionMethod, Module } from 'vuex'
import {
  // getToken,
  setToken,
  removeToken
  // setUserInfo,
  // removeUserInfo
} from '@/utils/auth'
import $api from '@/api/index'
// import { getUserInfo } from '@/utils/auth'
import * as mt from './mutations-types'

export default {
  namespaced: true,
  state: {
    token: '',
    info: {
      avatar: '',
      name: ''
    },
    LOADING: false,
    show: false,
    cachedViews: [],
    refresh: false,
    showTitlePaddingTop: false
  },
  mutations: {
    [mt.SET_TOKEN](state, data) {
      state.token = data
    },
    [mt.SET_USER_INFO](state, userData = {}) {
      state.info = userData
    },
    [mt.CLEAR_USER_INFO](state) {
      state.info = {}
      state.token = null
    },
    [mt.SHOW_LOADING](state, arg) {
      state.LOADING = arg
    },
    [mt.SHOW](state, arg) {
      state.show = arg
    },
    [mt.CATCH](state, arg) {
      if (state.cachedViews.indexOf(arg) === -1) {
        state.cachedViews.push(arg)
      }
    },
    [mt.DELCATCH](state, arg) {
      const index = state.cachedViews.indexOf(arg)
      index > -1 && state.cachedViews.splice(index, 1)
    },
    [mt.REFRESH](state, arg) {
      state.refresh = arg
    },
    [mt.SHOWTITLE](state, arg) {
      state.showTitlePaddingTop = arg
    }
  },
  actions: {
    async login({ commit }, data) {
      const result = await $api.user.login(data)
      commit(mt.SET_TOKEN, result.data.token)
      setToken(result.data.token)
    },
    async info({ commit }) {
      const result = await $api.user.info()
      commit(mt.SET_USER_INFO, result.data)
      // 用户信息每次进入都重新获取到session
      // setUserInfo(result.data);
    },
    logout({ commit }) {
      commit(mt.CLEAR_USER_INFO)
      removeToken()
      // removeUserInfo();
    },
    showLoading({ commit }, arg) {
      commit(mt.SHOW_LOADING, arg)
    },
    show({ commit }, arg) {
      commit(mt.SHOW, arg)
    },
    showView({ commit }, arg) {
      commit(mt.CATCH, arg)
    },
    delView({ commit }, arg) {
      commit(mt.DELCATCH, arg)
    },
    refresh({ commit }, arg) {
      commit(mt.REFRESH, arg)
    },
    showTitle({ commit }, arg) {
      commit(mt.SHOWTITLE, arg)
    }
  },
  getters: {
    token(state) {
      return state.token
    },
    info(state) {
      return state.info
    }
  }
}
